package com.vivo.tasks.vivofx;


import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.Office;
import com.thinkgem.jeesite.modules.sys.service.OfficeService;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.employee.entity.ViEmployee;
import com.vivo.employee.service.ViEmployeeService;
import com.vivo.fx.employee.entity.FxEmployeeinfo;
import com.vivo.fx.employee.service.FxEmployeeinfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class TaskViEmployee {

    private Logger logger = Logger.getLogger(TaskViEmployee.class);
    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Map<String, String> PeopleDict;

    public TaskViEmployee() {
        PeopleDict = new HashMap<String, String>();

        PeopleDict.put("EC180C3A50074365841ADA93756C7841", "3");
        PeopleDict.put("5ABBE292AE384E40ACD40E5AD01F71DA", "1");
        PeopleDict.put("C8EC6BE7782A4D2784791B1F3576DCDD", "2");

        PeopleDict.put("B76B77E89F6D47C29EEC9082B279429", "3");
        PeopleDict.put("220BB509254246E985A6FEA4A2060DAD", "1");
        PeopleDict.put("928B3F2CFD3F4B36A09FD704682BA06B", "2");
    }



    @Autowired
    private FxEmployeeinfoService fxEmployeeinfoService;

    @Autowired
    private ViEmployeeService viEmployeeService;

    public void SyncData() {
        getEmployeeInfo("ah");
        getEmployeeInfo("js");
    }

    private void getEmployeeInfo(String area) {
        System.out.println("get employee info from " + area + " ...");

        List<FxEmployeeinfo> fxEmployeeinfos = null;
        try {
            // 切换数据库
            Helper.changeDB(area);

//            fxEmployeeinfos = fxEmployeeinfoService.findList(new FxEmployeeinfo());

            int pageStart = 1;

            Page<FxEmployeeinfo> page = new Page<FxEmployeeinfo>(pageStart, 3000);

            Page<FxEmployeeinfo> pageRes = fxEmployeeinfoService.findPage(page, new FxEmployeeinfo());

            DatabaseContextHolder.setMainDb();
            saveEmployee(pageRes.getList(), area);

            System.out.println("page count is : " + pageRes.getTotalPage());

            for (int i = pageStart; i <= pageRes.getTotalPage(); i++) {
                System.out.println("cur page is :" + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());
                page.setPageNo(i);

                Helper.changeDB(area);
                pageRes = fxEmployeeinfoService.findPage(page, new FxEmployeeinfo());

                DatabaseContextHolder.setMainDb();
                saveEmployee(pageRes.getList(), area);
            }

        } finally {
            DatabaseContextHolder.setMainDb();
        }

        System.out.println("get employee info from " + area + " end !");
    }


    @Autowired
    private OfficeService officeService;

    private void saveEmployee(List<FxEmployeeinfo> empList, String area) {


        Office company = new Office();
        Office office = new Office();
        Office postion = new Office();

        ViEmployee sp = new ViEmployee();

        Map<String, String> officeMap = new HashMap<String, String>();

        List<Office> officesList = officeService.findMyOwnAllList();
        for (Office of: officesList) {
            officeMap.put(of.getId(), of.getName());
        }
        System.out.println("get office list size is:" + officesList.size());

        for (FxEmployeeinfo fe: empList) {
            String employeeId = fe.getEmployeeid();
            String superId = fe.getSuperioremployeeid();
//            if (area.equals("ah")) {
//                employeeId = employeeId.toLowerCase();
//                if (superId != null) {
//                    superId = superId.toLowerCase();
//                }
//            }
            ViEmployee ve = viEmployeeService.get(employeeId);

            company.setId(fe.getCompanyid());
            office.setId(fe.getDepartmentid());
            postion.setId(fe.getPositionid());

            String postName = officeMap.get(fe.getPositionid());


            sp.setId(superId);


            if (ve == null) {
                ve = new ViEmployee();
                ve.setIsTaskInsert(true);

                ve.setPostName(postName);

//                ve.setId(fe.getEmployeeid());
                ve.setId(employeeId);
                ve.setComp(company);
                ve.setDept(office);
                ve.setPost(postion);
                ve.setCode(fe.getEmployeecode());

                ve.setName(fe.getEmployeename());
                ve.setSex(fe.getSex());
                ve.setEducateId(fe.getEducationid());
                ve.setAge(fe.getAge());
                ve.setBirthDate(fe.getBirthdaydate());

                ve.setTelephone(fe.getTelephone());
                ve.setIdentityCard(fe.getIdentitycard());
                ve.setIndutyDate(fe.getIndutydate());

                String empAttrStr = "2";
                if (fe.getPeopleattribute() != null) {
                    empAttrStr = PeopleDict.get(fe.getPeopleattribute());
                    if (empAttrStr == null) {
                        empAttrStr = "2";
                    }
                }
//                System.out.println(empAttrStr);
                ve.setEmpAttribute(empAttrStr);

                ve.setDgAttribute(fe.getEmployeeattribute());


                ve.setCurStateId(fe.getEmployeecurrstateid());

                ve.setCensusRegister(fe.getCensusregister());
                ve.setAddress(fe.getAddress());
                ve.setRemarks(fe.getRemarks());
                ve.setSp(sp);
                ve.setSchool(fe.getSchool());

                ve.setProfessional(fe.getProfessional());
                ve.setEmergencyName(fe.getEmergencycontactname());
                ve.setEmergencyPhone(fe.getEmergencycontactphone());
                ve.setProfessional(fe.getProfessional());
                ve.setWageBase(fe.getWagebase());

                ve.setSocialNo(fe.getSocialnumber());
                ve.setSocialCurMoney(fe.getSocialcurrmoney());
                ve.setOutdutyDate(fe.getOutdutydate());

                ve.setCardNo(fe.getCardno());
                ve.setDepositBank(fe.getDepositbank());

                ve.setMaritalStatus(fe.getMaritalstatus());

                ve.setClothesSizeId(fe.getClothessizeid());

                ve.setZone(area);

                try {
                    viEmployeeService.save(ve);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            } else {
                // 查看是否离职
                if (fe.getOutdutydate() != null) {
                    ve.setOutdutyDate(fe.getOutdutydate());
                    ve.setBeginOutdutyDate(fe.getOutdutyoperatetime());

                    viEmployeeService.save(ve);
                }

                System.out.println("has exists with: " + employeeId + "   area is :" + area);
            }
        }
    }
}
